package servlet;

import entity.Classify;
import entity.OrderStatus;
import entity.Userinfo;
import service.ClassifyService;
import service.OrderStatusService;
import service.UserinfoService;
import service.impl.ClassifyServiceImpl;
import service.impl.OrderStatusServiceImpl;
import service.impl.UserinfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/UserInfoServlet")
public class UserInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //创建session对象
        HttpSession session = request.getSession();
        String opr = request.getParameter("opr");

        if(opr.equals("login")){//登入查询：账号&密码
            String userAccount = request.getParameter("userAccount");//账号
            String password = request.getParameter("password");//密码
            //调用查询方法
            UserinfoService service=new UserinfoServiceImpl();
            Userinfo userinfo = service.selectOne(userAccount, password);
            //判断返回结果
            PrintWriter out = response.getWriter();

            if(userinfo!=null){
                //传入商品分类
                ClassifyService service1=new ClassifyServiceImpl();
                List<Classify> selectclassify = service1.selectclassify();
                session.setAttribute("classify",selectclassify);

                OrderStatusService statusService=new OrderStatusServiceImpl();
                List<OrderStatus> list = statusService.selectAll();
                session.setAttribute("OrderStatus",list);
                //传入登录的用户对象
                session.setAttribute("userInfo",userinfo);
                out.print(true);
            }else {
                out.print(false);
            }

        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
